package com.wn.date20190806ATM;

import java.util.Scanner;

/**
 * ATM
 * 
 * @author song
 *
 */
public class ATM
{
	Scanner scanner = new Scanner(System.in);// 输入
	UserManager userManager = new UserManager();// 用户管理
	AccountManager accountManager = new AccountManager();// 账户管理

	public void mainMenu()
	{
		System.out.println("==============欢迎使用蜗牛ATM===============");
		System.out.println("===请输入你的操作，1：登陆  2：注册  3：退出 ===");
		System.out.println("============================================");

		int option = scanner.nextInt();
		switch (option)
		{
		case 1:
			login();
			break;
		case 2:
			signIn();
			break;
		case 3:
			System.out.println("再见！");
			scanner.close();
			System.exit(0);
			break;
		default:
			break;
		}

	}

	/**
	 * 注册界面
	 */
	private void signIn()
	{
		// TODO Auto-generated method stub
		User user = new User();
		// 输入密码
		while (true)
		{
			System.out.println("请输入你的账户");
			user.setAccount(scanner.next());
			System.out.println("请输入你的密码");
			user.setPassword(scanner.next());
			System.out.println("请再次输入你的密码");
			String confirmPassword = scanner.next();
			
			// 确认密码
			if (confirmPassword.equals(user.getPassword()))
			{
				if (userManager.register(user))
				{
					break;
				}
			} else
			{
				System.out.println("两次密码输入不一致！");
			}
		}
		System.out.println("注册成功！请登录");
		login();// 登陆

	}

	/**
	 * 登陆后菜单
	 */
	private void subMenu()
	{
		// TODO Auto-generated method stub
		System.out.println("===请输入你的操作，1：余额查询  2：存款  3：取款  4：转账  5.注销 ===");
		int option=scanner.nextInt();
		User user=Gloabl.getLoginUser();//获取登陆用户
		if(user!=null)
		{
			switch (option)
			{
			// 余额查询
			case 1:
				System.out.println(accountManager.query(user.getAccount()));
				subMenu();
				break;
			// 存款
			case 2:
				System.out.println("请输入存款金额");
				double money=scanner.nextDouble();
				if(accountManager.deposit(user.getAccount(), money))
				{
					System.out.println("存款成功");
				}
				subMenu();
				break;
			//取款
			case 3:
				System.out.println("请输入取款金额");
				double money2=scanner.nextDouble();
				if(accountManager.withdraw(user.getAccount(), money2))
				{
					System.out.println("取款成功");
				}
				subMenu();
				break;
			// 转账
			case 4:
				System.out.println("请输入转款账户");
				String to=scanner.next();
				System.out.println("请输入转账金额");
				double money3=scanner.nextDouble();
				if(accountManager.transfer(user.getAccount(), to, money3))
				{
					System.out.println("转账成功");
				}
				subMenu();
				break;
			// 注销
			case 5:
				user.setLogined(false);
				mainMenu();
				break;
			//
			default:
				subMenu();
				break;
			}
		}else {
			System.out.println("请先登录");
			login();
		}
		
	}

	/**
	 * 登陆界面
	 */
	private void login()
	{
		// TODO Auto-generated method stub
		System.out.println("请输入你的账户");
		String acc=scanner.next();
		System.out.println("请输入你的密码");
		String psw=scanner.next();
		if(userManager.login(acc, psw))
		{
			subMenu();
		}
		else {
			System.out.println("是否继续：1.重新登陆  2.主菜单 ");
			int option=scanner.nextInt();
			switch (option)
			{
			case 1:
				login();
				break;
			case 2:
				mainMenu();
				break;
			default:
				mainMenu();
				break;
			}
		}
	}
}
